package _2获取资源的绝对路径;

import java.io.*;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/getpath")
public class GetFilePathServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 得到上下文对象
		ServletContext context = this.getServletContext();

		// 得到文件在服务器上的真实磁盘路径
		String path = context.getRealPath("/WEB-INF/1.txt");
		System.out.println(path);

		Reader r = new FileReader(path);
		char[] buf = new char[1024];
		int len = 0;
		while ((len = r.read(buf)) != -1) {
			String l = new String(buf, 0, len);
			resp.getWriter().print(l + "<br />");
		}
		r.close();

	}
}
